package com.dormitoryAdministratorSystem.ui.classmanage;

import com.dormitoryAdministratorSystem.entity.clazz.ClassBean;
import com.dormitoryAdministratorSystem.entity.page.PageBean;
import com.dormitoryAdministratorSystem.service.clazz.impl.ClassSerImpl;
import com.dormitoryAdministratorSystem.service.student.impl.StudentSerImpl;
import com.dormitoryAdministratorSystem.ui.MainFrame;
import com.lovo.netCRM.component.LovoButton;
import com.lovo.netCRM.component.LovoTable;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

public class ClassManagePanel extends JPanel {
	private MainFrame frame;
	
	private LovoTable classTab;
	
	private LovoButton classInBtn;
	private LovoButton classOutBtn;
	private LovoButton preBtn;
	private LovoButton nextBtn;
	private JLabel  pageLab;

	private ClassSerImpl csi = new ClassSerImpl();
	private StudentSerImpl ssi = new StudentSerImpl();
	private Map<String,String> con = new HashMap<>();
	PageBean<ClassBean> cPage = new PageBean<>();
	
	public ClassManagePanel(MainFrame frame){
		this.frame = frame;
		this.setBackground(Color.WHITE);
		this.setLayout(null);
		this.initTable();
		this.initBtn();
	}

	private void initTable() {
		// TODO Auto-generated method stub
		this.classTab = new LovoTable(this, new String[]{"班级名称","开班时间","带班老师"},
				new String[]{"cName", "openTime", "teacherName"},//数组中应该是对应属性的属性名
				"cId");//填入唯一标示属性
		this.classTab.setSizeAndLocation(20, 0, 750, 300);
		cPage = csi.showClassesInfo(con);
		this.classTab.updateLovoTable(cPage.getDates());
	}

	private void initBtn() {


		this.preBtn = new LovoButton("上一页",220,320,this);
		this.nextBtn = new LovoButton("下一页",490,320,this);
		this.pageLab = new JLabel("页数" + cPage.getCurPage() + "/" + cPage.getTolSize() + ",共" + cPage.getTolNum() + "条"
				+ "每页" + cPage.getPageSize() + "条");
		this.pageLab.setBounds(315,320,300,20);
		this.add(this.pageLab);
		this.add(this.preBtn);
		this.add(this.nextBtn);

		// TODO Auto-generated method stub
		this.classInBtn = new LovoButton("添加班级", 220, 380, this);
		this.classOutBtn = new LovoButton("删除班级", 450, 380, this);
		
		this.classInBtn.addActionListener(new ActionListener() {			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				new ClassAddDialog(frame);
				updateCon(con.get("cname"),con.get("teacher"),con.get("sTime"),cPage.getCurPage()+"",
						cPage.getPageSize()+"");
			}
		});
		
		this.classOutBtn.addActionListener(new ActionListener() {			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				int cId = classTab.getKeyByInt();
				if(ssi.haveStudentsInClass(cId)){
					new ClassDelDialog(frame);
				}else{
					int isSure = JOptionPane.showConfirmDialog(null, "确定要删除吗");
					if(isSure == 0){
						if(csi.deleClass(cId) == 1){
							JOptionPane.showMessageDialog(null,"删除成功");
						}else{
							JOptionPane.showMessageDialog(null,"删除失败");
						}
					}
				}
				updateCon(con.get("cname"),con.get("teacher"),con.get("sTime"),cPage.getCurPage()+"",
						cPage.getPageSize()+"");
			}
		});

		//上一页
		this.preBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(cPage.getCurPage() == 1){
					JOptionPane.showMessageDialog(null,"没有上一页了");
				}else{
					updateCon(con.get("cname"),con.get("teacher"),con.get("sTime"),(cPage.getCurPage() - 1)+"", cPage.getPageSize()+"");
				}
			}
		});

		//下一页
		this.nextBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(cPage.getTolSize() == cPage.getCurPage()){
					JOptionPane.showMessageDialog(null, "没有下一页了");
				}else{
					updateCon(con.get("cname"),con.get("teacher"),con.get("sTime"), (cPage.getCurPage() + 1)+"", cPage.getPageSize() + "");
				}
			}
		});
	}

	private void updateCon(String cname, String teacher, String sTime, String curPage, String pageSize){
		con.put("cname", cname);
		con.put("teacher", teacher);
		con.put("sTime", sTime);
		con.put("curPage", curPage);
		con.put("pageSize", pageSize);
		cPage = csi.showClassesInfo(con);
		classTab.updateLovoTable(cPage.getDates());
		this.pageLab.setText("页数" + cPage.getCurPage() + "/" + cPage.getTolSize() + ",共" + cPage.getTolNum() + "条"
				+ "每页" + cPage.getPageSize() + "条");
	}
	
}
